import { loginByUsername, logout, getUserMenu, getUserInfo } from '@/api/login'
import { getToken, setToken, removeToken, setUserInformation, removeUserInformation, setUserInfro, removeUserInfro } from '@/utils/auth'

const user = {
  state: {
    code: '',
    token: getToken()
  },

  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USER_INFRO: (state, userInfro) => {
      state.userInfro = userInfro
    },
    SET_USERINFORMATION: (state, userInformation) => {
      state.userInformation = userInformation
    }
  },

  actions: {
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const loginPassword = userInfo.password
      return new Promise((resolve, reject) => {
        loginByUsername(username, loginPassword).then(response => {
          console.log(response)
          if (response.status === 200) {
            const data = response.data
            const token = data.token_type + ' ' + data.access_token
            getUserInfo(token).then(res => {
              const resdata = res.data.data
              getUserMenu(token).then(res => {
                console.log('getUserMenu', res)
                const menu = res.data.data
                const loginadmin = getNewArr(menu)
                const loginText = JSON.stringify(loginadmin)
                commit('SET_TOKEN', token)
                commit('SET_USER_INFRO', loginadmin)
                commit('SET_USERINFORMATION', resdata)
                setUserInfro(loginText)
                setToken(token)
                setUserInformation(resdata)
                resolve()
              })
            })
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_USER_INFRO', '')
          removeUserInfro()
          commit('SET_TOKEN', '')
          commit('SET_USERINFORMATION', '')
          removeToken()
          removeUserInformation()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_USER_INFRO', '')
        removeUserInfro()
        commit('SET_TOKEN', '')
        commit('SET_USERINFORMATION', '')
        removeToken()
        removeUserInformation()
        resolve()
      })
    }
  }
}

function getNewArr(arr) {
  const result = []
  for (let i = 0; i < arr.length; i++) {
    const temp = getNemu(arr[i])
    result.push(temp)
  }
  return result
}

function getNemu(object) {
  const result = {}
  if (object.permResource.indexOf('/**') !== -1) {
    result.path = object.permResource.replace('/**', '')
  } else {
    result.path = object.permResource
  }
  result.name = object.menuName

  const children = object.childList
  if (children && children.length !== 0) {
    const temp = []
    for (let i = 0; i < children.length; i++) {
      const element = getNemu(children[i])
      temp.push(element)
    }
    result.children = temp
  } else {
    result.children = []
  }
  return result
}

export default user
